var express = require('express')
var router = express.Router()
const { getAll, getById, exec } = require('../db/index')

// GET /touristCate
router.get('/', async (req, res) => {
  let sql = `select * from category`

  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '获取所有分类成功',
    result: {
      data,
    },
  })
})

//获取某一分类下的所有文章
// GET /touristCate/articles
router.get('/articles/:id', async (req, res) => {
  const { id } = req.params
  let sql = `select * from articles where cate_id = ${id}`

  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '获取所有文章成功',
    result: {
      data,
    },
  })
})

//获取某一篇文章的具体内容
// /touristCate/articleContent
router.get('/articleContent/:id', async (req, res) => {
  const { id } = req.params

  let sql = `select t1.* , t2.tag from articles as t1 ,category as t2 WHERE t1.id = ${id} AND t1.cate_id=t2.id`
  const data = await getById(sql)

  res.send({
    code: 0,
    message: '获取文章内容成功',
    result: {
      data,
    },
  })
})

//获取所有文章
router.get('/allArticles',async (req,res) => {
  let sql = `select * from articles`
  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '获取所有文章成功',
    result: {
      data,
    },
  })
})

//根据title搜索具体文章
router.get('/visitorSearch',async (req,res) => {
  const {title} = req.query
  console.log(title)
 
  let sql = `select * from articles where title like '%${title}%'`
  console.log(sql)

  const data = await getAll(sql)
  res.send({
    code:0,
    message:'搜索文章成功',
    result:data
  })
})

//获取所有留言
router.get('/message', async (req, res) => {
  let sql = `select * from message order by time desc;`

  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '获取所有评论成功',
    result: {
      data,
    },
  })
})
//添加留言
router.post('/message', async (req, res) => {
  const { content } = req.body
  let sql = `insert into message (content) values ('${content}')`

  const data = await getAll(sql)

  res.send({
    code: 0,
    message: '已收到小主评论',
  })
})

//添加点击量
router.get('/addBrowse',async (req,res) => {
  const {id} = req.body
  let sql = `update art`
})

module.exports = router
